#!/usr/bin/env python3

import os
import subprocess
import tempfile
from generate_gitee_urls import get_gitee_urls

def clone_and_tag_repo(repo_url):
    # repo_url = "https://gitee.com/agiros/acado-vendor"
    tag_base = "20250617"
    branches = ["openEuler2403lts", "ubuntu2204lts"]
    
    # 创建临时目录
    with tempfile.TemporaryDirectory() as temp_dir:
        # 克隆仓库
        subprocess.run(["git", "clone", repo_url, temp_dir], check=True)
        
        # 切换到仓库目录
        os.chdir(temp_dir)
        
        for branch in branches:
            # 切换到目标分支
            subprocess.run(["git", "checkout", branch], check=True)
            
            # 为每个分支创建唯一的标签名称
            branch_tag = f"{tag_base}-{branch}"
            
            # 为当前分支创建标签
            subprocess.run(["git", "tag", "-a", branch_tag, "-m", f"Tag {branch_tag} for {branch} branch"], check=True)
            
            # 推送标签到远程仓库
            subprocess.run(["git", "push", "origin", branch_tag], check=True)
            
        print(f"Successfully tagged branches {branches} with unique tags")

def single_set_gitee_tags():
    urls = "https://gitee.com/agiros/acado-vendor"
    clone_and_tag_repo(url)


def batch_set_gitee_tags():
    urls = get_gitee_urls(True)
    for url in urls:
        clone_and_tag_repo(url)


if __name__ == "__main__":
    try:
        single_set_gitee_tags()
    except subprocess.CalledProcessError as e:
        print(f"Error occurred: {e}")
        print("Please make sure you have proper access rights to the repository")
    except Exception as e:
        print(f"Unexpected error: {e}")